package stark.app.frame.rxpermission;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import stark.app.R;
import stark.common.basic.utils.PermissionUtil;

public class PermissionDemoActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "PermissionDemoActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission_demo);


        findViewById(R.id.btnSingle).setOnClickListener(this);
        findViewById(R.id.btnMore).setOnClickListener(this);
        findViewById(R.id.btnMoreResult).setOnClickListener(this);
        findViewById(R.id.btnSetting).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.btnSingle) {
            PermissionUtil.with(this).permission(Manifest.permission.CAMERA)
                    .request(granted -> {
                        if (granted) {
                        } else {
                        }
                    });
        } else if (id == R.id.btnMore) {
            PermissionUtil.with(this).permission(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
                    .request(granted -> {
                        if (granted) {
                            Log.e(TAG, "onClick: 全部允许");
                            // All requested permissions are granted
                        } else {
                            Log.e(TAG, "onClick:  某个权限被拒绝");
                            // At least one permission is denied
                        }
                    });
        } else if (id == R.id.btnMoreResult) {
            PermissionUtil.with(this)
                    .permission(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
                    .request(new PermissionUtil.ICallback() {
                        @Override
                        public void onGranted(List<String> permissions, boolean all) {
                        }

                        @Override
                        public void onDenied(List<String> permissions, boolean never) {
                        }
                    });
        } else if (id == R.id.btnSetting) {
            Intent intent = new Intent(
                    Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, 22);
        }
    }
}